package org.brakepedal.server.support.http;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Date: 2023/2/10 1:40
*/
@Getter
@Setter
@SuperBuilder
public class ComResponseBody extends AbsResponseBody {
    public static final AbsResponseBody SUCCESS;
    public static final AbsResponseBody WARN;
    public static final AbsResponseBody ERROR;

    public static final class FinalComResponseBody extends AbsResponseBody {
        private final ResponseBodyCode code;

        private final ResponseBodyStatus status;

        private final ResponseBodyMsg msg;

        private final Object data;

        {
            this.data = null;
        }

        public FinalComResponseBody(ResponseBodyCode code, ResponseBodyStatus status, ResponseBodyMsg msg) {
            this.code = code;
            this.status = status;
            this.msg = msg;
        }

        @Override
        public ResponseBodyCode getCode() {
            return code;
        }

        @Override
        public ResponseBodyStatus getStatus() {
            return status;
        }

        @Override
        public ResponseBodyMsg getMsg() {
            return msg;
        }

        @Override
        public Object getData() {
            return data;
        }
    }

    @Nullable
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String errorType;

    static {
        SUCCESS = new FinalComResponseBody(ResponseBodyCode.SUCCESS, ResponseBodyStatus.SUCCESS, ResponseBodyMsg.DEFAULT_SUCCESS);
        WARN = new FinalComResponseBody(ResponseBodyCode.BAD_REQUEST, ResponseBodyStatus.WARN, ResponseBodyMsg.DEFAULT_WARN);
        ERROR = new FinalComResponseBody(ResponseBodyCode.BAD_REQUEST, ResponseBodyStatus.ERROR, ResponseBodyMsg.DEFAULT_ERROR);
    }
    public ComResponseBody() {
    }

    public ComResponseBody(ResponseBodyMsg msg) {
        setMsg(msg);
    }

    public ComResponseBody(ResponseBodyCode code, ResponseBodyStatus status, ResponseBodyMsg msg, Object data) {
        super(code, status, msg, data);
    }

    public ComResponseBody(ResponseBodyCode code, ResponseBodyStatus status, ResponseBodyMsg msg) {
        super(code, status, msg);
    }

    @Contract("_ -> new")
    public static @NotNull AbsResponseBody makeSuccessMsg(ResponseBodyMsg msg) {
        return new ComResponseBody(msg);
    }

    @Contract("_ -> new")
    public static @NotNull AbsResponseBody warnMsg(ResponseBodyMsg msg){
        return new ComResponseBody(ResponseBodyCode.BAD_REQUEST, ResponseBodyStatus.WARN,msg);
    }

    public static AbsResponseBody warnMsg(String msg) {
        return new ComResponseBody(ResponseBodyCode.BAD_REQUEST, ResponseBodyStatus.WARN,ResponseBodyMsg.custom(msg));
    }

    @Contract("_, _ -> new")
    public static @NotNull AbsResponseBody warnMsg(ResponseBodyMsg msg, ResponseBodyCode code){
        return new ComResponseBody(code, ResponseBodyStatus.WARN,msg);
    }

    @Contract("_ -> new")
    public static @NotNull AbsResponseBody errorMsg(ResponseBodyMsg msg){
        return new ComResponseBody(ResponseBodyCode.BAD_REQUEST, ResponseBodyStatus.ERROR,msg);
    }

    @Contract("_ -> new")
    public static @NotNull AbsResponseBody errorMsg(String msg){
        return new ComResponseBody(ResponseBodyCode.BAD_REQUEST, ResponseBodyStatus.ERROR,ResponseBodyMsg.custom(msg));
    }
    @Contract("_, _ -> new")
    public static @NotNull AbsResponseBody errorMsg(ResponseBodyMsg msg, ResponseBodyCode code){
        return new ComResponseBody(code, ResponseBodyStatus.ERROR,msg);
    }

    public static AbsResponseBody makeSuccessData(Object data) {
        return new ComResponseBody().setData(data);
    }

    public static ComResponseBody ok(Object data) {
        return (ComResponseBody) new ComResponseBody().setData(data);
    }

    public static ComResponseBody ok() {
        return ok(null);
    }

    @Override
    public String toString() {
        return "ComResponseBody"+super.toString();
    }
}
